<?php

class Login extends CT_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index(){
        
        $this->load->model('nursery_admin_model');
        $this->load->model('nursery_model');
        $this->load->library('CT_auth_nursery');
        $this->load->library('form_validation');
        
        if( $this->input->is_method('POST')){ 
            if ( $this->form_validation->run('login') == true ){  
                $code = $this->input->post('code');
                if (! $code || strtolower($code) != strtolower($this->session->userdata('captcha'))){
                
                    $this->form_validation->set_field_error('code', '验证码错误');
                }else{    
                    $username = $this->input->post('username');
                    $password = $this->input->post('password');
                    
                    $user = $this->nursery_admin_model->find(array('username' => $username, 'status'=>0));
                    if (!empty($user) && $user['username'] == $username && $user['pass'] == $this->ct_auth_nursery->hashSSHA($password) && $user['status'] == 0) {
                    
                        // 主账户信息
                        $nursery = $this->nursery_model->find(array(
                            'id' => $user['nursery_id'],
                            'status' => 1
                        ));
                        
                        $sess_info = [
                            'id' => $nursery['id'],
                            'title' => $nursery['title'],
                            'rid' => $user['rid'],
                            'uid' => $user['id'],
                            'username' => $user['username']
                        ];
                        
                        $this->ct_auth_nursery->set_nursery_user_login($sess_info);
                    
                        if ( $this->ct_auth_nursery->is_nursery_login() ) {
                            $return_url = $this->input->get('url', TRUE) ? urldecode($this->input->get('url', TRUE)) : '';
                            
                            if($return_url){
                                redirect($return_url);
                            }else{
                                redirect('/nursery/index');
                            }
                        }
                    }else{
                        $this->form_validation->set_field_error('password', '用户名或密码错误');
                    }
                }
            }              
        }
        
        $this->load->view('nursery/login/index');   
    }
    
    public function logout()
    {
        $this->load->library('CT_auth_nursery');
        $this->ct_auth_nursery->set_nursery_user_logout();
        $this->load->helper('url');
        redirect('/nursery/login');
    }

    public function captcha() {
        $this->load->helper('my_captcha');
        $captcha = new Captcha();
        $text = $captcha->GetCaptchaText();
        $this->session->set_userdata('captcha', $text);
        $captcha->CreateImage($text, $savePath = null);
        exit;
    }

}
